package com.dw.dwdzcommerce.view.mainContent.ad;

import java.util.ArrayList;
import java.util.List;

import com.dw.dwdzcommerce.R;
import com.dw.dwdzcommerce.base.activity.BaseActivity;
import com.dw.dwdzcommerce.tools.commons.L;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

public class ADCoinStageLayout {
	private static final int BOTTOM_MARGIN = 16;
	private ViewGroup mStageRootLayout;
	private List<View> mChildViewLis;
	private Context mContext;
	private int mSize;

	public void initModule(BaseActivity context, RelativeLayout root) {
		mContext = context;
		if (mStageRootLayout == null) {
			mStageRootLayout = (ViewGroup) root.findViewById(R.id.item_adpager_lin);
		}

	}

	/**
	 * 根据大小初始化子view的数目并显示
	 * 
	 * @param size
	 */
	public void initSize(int size) {
		mSize = size;
		mChildViewLis = new ArrayList<View>(size);
		initChildView();
		initRootView();
	}

	public int getItemSize() {
		return mSize;
	}

	/**
	 * 初始化父控件
	 */
	private void initRootView() {
		mStageRootLayout.post(new Runnable() {

			@Override
			public void run() {
				ViewGroup.LayoutParams params = mStageRootLayout.getLayoutParams();
				params.height = BOTTOM_MARGIN * 3;
				mStageRootLayout.setLayoutParams(params);

			}
		});

	}

	/**
	 * 初始化子view
	 */
	private void initChildView() {
		try {
			for (int i = 0; i < mSize; i++) {
				View child = generateView(i);
				mStageRootLayout.addView(child);
				mChildViewLis.add(child);
			}
		} catch (NullPointerException exception) {
			L.e(exception);
		}
	}

	/**
	 * 创建点
	 * 
	 * @return
	 */
	private View generateView(int i) {
		ImageView btn = new ImageView(mContext);
		btn.setTag(i);
		btn.setLayoutParams(new LinearLayout.LayoutParams(10, 10));
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mListener != null) {
					mListener.onselect(((Integer) v.getTag()));
				}
			}
		});
		btn.setBackgroundResource(R.drawable.coin_bg_unpress);
		LinearLayout.LayoutParams params = (LayoutParams) btn.getLayoutParams();
		params.rightMargin = 10;
		params.leftMargin = 5;
		params.bottomMargin = BOTTOM_MARGIN;
		btn.setLayoutParams(params);

		return btn;
	}

	/**
	 * 初始化点
	 */
	public void release() {
		mStageRootLayout.removeAllViews();
		mChildViewLis.clear();
	}

	public void select(int index) {
		closeAll();
		choose(index);
	}

	/**
	 * 选择
	 * 
	 * @param index
	 */
	private void choose(int index) {
		try{
		View view = mChildViewLis.get(index);
		view.setBackgroundResource(R.drawable.coin_bg_pressed);
		}catch(NullPointerException e){
			L.e(e);
		}
	}

	private void closeAll() {
		try {
			for (int i = 0; i < mChildViewLis.size(); i++) {
				View view = mChildViewLis.get(i);
				view.setBackgroundResource(R.drawable.coin_bg_unpress);
			}
		} catch (NullPointerException e) {
			L.e(e);
		}
	}

	private OnClickSelect mListener;

	public void setmListener(OnClickSelect mListener) {
		this.mListener = mListener;
	}

	public interface OnClickSelect {
		void onselect(int index);
	}

}
